/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 */
package top.hiai._admin.user;

import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import top.hiai._admin.role.RoleAdminService;
import top.hiai.common.model.Role;
import top.hiai.common.model.User;
import top.hiai.config.BaseController;

import java.util.List;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍
*/

public class UserAdminController extends BaseController {
	UserAdminService userAdminService = UserAdminService.me;
	
	/**
	 * 后台用户列表页面
	 */
	public void index() {
		Page<User> userPage = userAdminService.paginate(getParaToInt("p", 1),limtCount);
		setAttr("userPage", userPage);
		render("userList.html");
	}
	/**
	 * 账户锁定
	 */
	public void lock() {
		Ret ret = userAdminService.lock(getLoginAccountId(), getParaToInt("id"));
		renderJson(ret);
	}

	/**
	 * 账户解锁
	 */
	public void unlock() {
		Ret ret = userAdminService.unlock(getParaToInt("id"));
		renderJson(ret);
	}
	
	/**
	 * 显示修改用户的界面
	 */
	public void userEdit(){
		keepPara("p");	// 保持住分页的页号，便于在 ajax 提交后跳转到当前数据所在的页
		User user = userAdminService.findById(getParaToInt("id"));
		setAttr("user", user);
		render("userEdit.html");
	}
	/**
	 * 提交修改
	 */
	@Before(UserAdminValidator.class)
	public void userUpdate() {
		User User = getBean(User.class);
		Ret ret = userAdminService.userUpdate(User);
		renderJson(ret);
	}
	/**
	 * 分配角色
	 */
	public void assignRoles() {
		User user = userAdminService.findById(getParaToInt("id"));
		List<Role> roleList = RoleAdminService.me.getAllRoles();
		userAdminService.markAssignedRoles(user, roleList);
		setAttr("user", user);
		setAttr("roleList", roleList);
		render("assign_roles.html");
	}
	
	/**
	 * 添加角色
	 */
	public void addRole() {
		Ret ret = userAdminService.addRole(getParaToInt("accountId"), getParaToInt("roleId"));
		renderJson(ret);
	}

	/**
	 * 删除角色
	 */
	public void deleteRole() {
		Ret ret = userAdminService.deleteRole(getParaToInt("accountId"), getParaToInt("roleId"));
		renderJson(ret);
	}
}